/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: CardPagerAdapter
 * Author: Clark
 * Date: 2020/10/14 15:41
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardRes;
import sz.szsmk.citizencard.model.network.clarkbean.response.AdInfo;

/**
 * @ClassName: CardPagerAdapter
 * @Description:
 * @Author: Clark
 * @Date: 2020/10/14 15:41
 */
public class CardPagerAdapter extends PagerAdapter {

    //显示的数据
    List<QueryUserCardRes.DataBean> dataBeanList = new ArrayList<>();
    private List<View> viewList=new ArrayList<>();
    private Context mContext;
    public CardPagerAdapter(Context context, View V){
        mContext=context;
    }
    private BaseInterface baseInterface;
    public interface BaseInterface {
        public void onClick(View view,  QueryUserCardRes.DataBean bean);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }
    @Override
    public int getCount() {
        return dataBeanList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @Override
    public int getItemPosition(Object object){
        return POSITION_NONE;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewList.get(position));
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view;
        if (viewList.size() == position) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_card_view, null, false);
            viewList.add(view);
        } else {
            view = viewList.get(position);
        }
        view.setTag(position);

        TextView cardType = view.findViewById(R.id.card_type);
        TextView cardTypeEn=view.findViewById(R.id.card_type_en);
        TextView cardNum=view.findViewById(R.id.card_serial_num);
        RelativeLayout cardBack=view.findViewById(R.id.card_back);
        QueryUserCardRes.DataBean userCardBean = this.dataBeanList.get(position);
        List<QueryUserCardRes.DataBean.CardDetailInfolistBean> cardDetailInfolistBeanList = userCardBean.getCardDetailInfolist();
        if (cardDetailInfolistBeanList.isEmpty()) {
            return view;
        }
        QueryUserCardRes.DataBean.CardDetailInfolistBean bean = cardDetailInfolistBeanList.get(0);

        String cardNo = userCardBean.getBindCardNo();
        String cType = bean.getCardTypeName();
        String cTypeEn = bean.getCardNickName();
        StringBuilder str = new StringBuilder(cardNo.replace(" ", ""));
        if (cType.contains("B")) {
            cardBack.setBackground(mContext.getResources().getDrawable(R.drawable.card_back_bk));
        }
//        if(cType.contains("省交通一卡通")){cardBack.setBackground(mContext.getResources().getDrawable(R.drawable.card_back_jssjt));}
        if (cType.contains("交通一卡通")) {
        }
        if (cType.contains("社会保障")) {
        }


        int i = str.length() / 4;
        int j = str.length() % 4;
        for (int x = (j == 0 ? i - 1 : i); x > 0; x--) {
            str = str.insert(x * 4, " ");
        }
        cardNum.setText(str);

        //cardTypeEn.setText(cTypeEn);
        cardBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                baseInterface.onClick(v, dataBeanList.get(position));
            }
        });

        container.addView(view);

        return view;
    }
    public void setData(List<QueryUserCardRes.DataBean> data) {
        this.dataBeanList.clear();
        this.dataBeanList.addAll(data);
        notifyDataSetChanged();
    }
}